<?php

namespace MSDK\Controller;

/**
 * RoleController.class 控制器
 * 
 * @since 1.0 <2015-5-23> SoChishun <14507247@qq.com> Added.
 */
class MSDKRoleController extends AppbaseController {

    /**
     * 获取role_list查询表单数据
     * @return array 包含search和where数组
     * @since 1.0<2015-5-23> SoChishun Added.
     */
    function role_list_search() {
        $search = I('get.');
        if ($search['keyword']) {
            $where['name'] = array('like', '%' . $search['keyword'] . '%');
        }
        return array('search' => $search, 'where' => $where);
    }

    /**
     * role_list操作
     * @since 1.0<2015-5-23> SoChishun Added.
     */
    public function role_list() {
        $this->display();
    }

    /**
     * 获取列表查询数据
     * @return array
     * @since 1.0 <2015-7-2> SoChishun Added.
     */
    function get_role_search_data() {
        $search = I();
        $where = false;
        if ($search['name']) {
            $where['name'] = array('LIKE', $search['name']);
        }
        if ($search['code']) {
            $where['code'] = array('LIKE', $search['code']);
        }
        return array('search' => $search, 'where' => $where);
    }

    public function get_role_tree_json() {
        $list = D('MSDKRole')->get_tree_list();
        $this->ajaxReturn($list);
    }

    /**
     * 获取列表JSON数据
     * @since 1.0 <2015-6-10> SoChishun Added.
     */
    public function get_role_list_json() {
        $search_data = $this->get_role_search_data();
        $list = D('MSDKRole')->get_tree_list('treetable', $search_data['where']);
        $this->ajaxReturn($list);
    }

    /**
     * 导出数据
     * @since 1.0 <2014-6-13> SoChishun Added.
     */
    public function role_export() {
        if (!$this->isSuperAdmin) {
            die('没有权限!');
        }
        $search_data = $this->get_role_search_data();
        $list = D('MSDKRole')->scope('export')->where($search_data['where'])->select();
        if (!$list) {
            $this->ajaxMsg(false, '找不到符合条件的记录!');
        }
        sofn_excel_export($list);
    }
    public function role_copy($id=0){
        if(!$id){
            $this->ajaxMsg(false,'参数无效');
        }
        M()->execute('insert into tuser_role (role_name,parent_id,role_code,permission_rule) select concat(role_name,\'_new\'),parent_id,role_code,permission_rule from tuser_role where id='.$id);
        $this->ajaxMsg(true);
    }

    /**
     * role_edit操作
     * @param int $id 主键编号
     * @since 1.0<2015-5-23> SoChishun Added.
     */
    public function role_edit($id = 0) {
        if ($id) {
            $this->assign('data', M('tuser_role')->find($id));
        }
        $this->display();
    }

    /**
     * role_edit_save操作
     * @since 1.0<2015-5-23> SoChishun Added.
     */
    public function save_role_json() {
        $this->ajaxReturn(D('MSDKRole')->save_role());
    }

    /**
     * role_delete操作
     * @param string $id 主键编号
     * @since 1.0<2015-7-31> SoChishun Added.
     */
    public function role_delete($id = '') {
        $this->ajaxReturn(D('MSDKRole')->delete_role($id));
    }

    public function role_permission_edit($id = 0) {
        $this->assign('id', $id);
        $this->display();
    }

    public function get_role_permission_list_json($id) {
        $rules = M('tuser_role')->where(array('id' => $id))->getField('permission_rule');
        $this->ajaxReturn(D('Permission')->get_tree_list($rules));
    }

    public function save_role_permission() {
        $id = I('id');
        $ids = I('ids');
        if (!$id || !$ids) {
            $this->ajaxMsg(false, '表单数据不完整');
        }
        $result = M('tuser_role')->where(array('id' => $id))->setField('permission_rule', $ids);
        $this->ajaxMsg($result, false === $result ? M('tuser_role')->getDbError() : '');
    }

}
